<template>
<form>
  <div class="form-group">
    <label for="userName">标题</label>
    <input type="text" class="form-control" v-model="title" >
  </div>
  <div class="form-group">
    <label for="password">内容</label>
    <textarea class="form-control" v-model="content"></textarea>
  </div>
  <button id="sub" type="button" @click="add" class="btn btn-primary">Submit</button>
  <button id="cancel" type="button" @click="back" class="btn btn-default">Cancel</button>
</form>
</template>

<script>
import Swal from 'sweetalert'
import {post} from '../../utils/utils.js'
export default {
  data () {
    return {
      title: '',
      content: ''
    }
  },
  methods:{
    back(){
      this.$root.back()
    },
    async add(){
      if(!this.title){
        Swal('', '请输入标题', 'error')
      }
      const res = await post('/publish/add', {title:this.title, content:this.content})
      if(res.err!=1){
        Swal('', '发表成功', 'success').then(()=>{
          this.jump('comp-home')
        })
      }
    },
    jump(compName, data){
      this.$root.chgComp(compName, data)
    }
  }
}
</script>

